package com.wandian.notepad.mvvm.notepadclassify;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.dialog.CommonDialogEdit;
import com.mylib.libcore.dialog.CommonDialogTextChoose;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.notepad.R;
import com.wandian.notepad.adapter.NotepadClassifyTypeRecycleAdapter;
import com.wandian.notepad.bean.NoteTypeBean;
import com.wandian.notepad.databinding.NotepadActivityClassifyBinding;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 记事本分类activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NOTEPAD_CLASSIFY)
public class NotepadClassifyActivity extends BaseMVVMActivity<NotepadClassifyActivityViewModel> implements
        CommonDialogEdit.CommonDialogEditBtnClink ,CommonDialogTextChoose.CommonDialogTextBtnClink {
    private NotepadActivityClassifyBinding dataBinding;
    private CommonDialogEdit commonDialogEdit;
    private CommonDialogTextChoose commonDialogTextChoose;
    private NotepadClassifyTypeRecycleAdapter notepadClassifyRecycleAdapter;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.notepad_activity_classify);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initRecycleView();
        initClink();
        mViewModel.notepadTypeList();
    }

    private void initRecycleView(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.rvClassify.setLayoutManager(layoutManager);
        notepadClassifyRecycleAdapter  = new NotepadClassifyTypeRecycleAdapter(R.layout.notepad_item_notes_select_classify);
        dataBinding.rvClassify.setAdapter(notepadClassifyRecycleAdapter);
        notepadClassifyRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if(i>3){
                    NoteTypeBean noteTypeBean = (NoteTypeBean)baseQuickAdapter.getData().get(i);
                    if(!UIUtils.isEmpty(noteTypeBean))showDialog("编辑备忘分类名称","notepad_edit",noteTypeBean.getId()+"");
                }
            }
        });
        notepadClassifyRecycleAdapter.setDialogViewClink(new NotepadClassifyTypeRecycleAdapter.BtnClink(){
            @Override
            public void viewBtnClink(String id) {
                showDeleteDialog("","notepad_delete",id);
            }
        });
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindSysTwoToolBar.toolBarTvTitle.setText("备忘分类管理");
        mViewModel.noteTypeList.observe(this,listBean->{
            if(!UIUtils.isEmpty(listBean)){
                notepadClassifyRecycleAdapter.setNewData(listBean);
                notepadClassifyRecycleAdapter.notifyDataSetChanged();
            }
        });
    }


    private void initClink(){
        dataBinding.bindSysTwoToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        dataBinding.btnAddClassify.setOnClickListener(v -> showDialog("新增备注分类名称","notepad_type",""));
    }

    //展示失败弹框
    private void showDialog(String title,String type,String id){
        try {
            commonDialogEdit = new CommonDialogEdit(this,title,type,id,this::btnClink);
            //显示窗口
            //设置layout在PopupWindow中显示的位置
            if(!UIUtils.isEmpty(dataBinding.llRoot)){
                commonDialogEdit.showAtLocation(dataBinding.llRoot, Gravity.CENTER, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //展示失败弹框
    private void showDeleteDialog(String title,String type,String id){
        try {
            commonDialogTextChoose = new CommonDialogTextChoose(this,title,type,id,this::sureClink);
            //显示窗口
            //设置layout在PopupWindow中显示的位置
            if(!UIUtils.isEmpty(dataBinding.llRoot)){
                commonDialogTextChoose.showAtLocation(dataBinding.llRoot, Gravity.CENTER, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        finishhideSoftBoard();
        super.onDestroy();
    }

    @Override
    public void btnClink(String text,String type,String id) {
        if(type.equals("notepad_type"))  mViewModel.addNotepadType(text);
        if(type.equals("notepad_edit"))  mViewModel.modifyNotepadType(text,id);
    }

    @Override
    public void sureClink(String text, String type, String id) {
        if(type.equals("notepad_delete"))  mViewModel.deleteNotepadType(id);
    }
}
